package com.fand.platform.authority.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fand.context.mapper.JsonMapper;
import com.fand.dto.PaginationDto;
import com.fand.platform.authority.dto.UserGrDto;
import com.fand.platform.authority.dto.UserGrBuArDto;
import com.fand.platform.authority.dto.UserGrRoDto;
import com.fand.platform.authority.dto.UserGrUsDto;
import com.fand.platform.configuration.service.ConfigurationService;
import com.fand.platform.authority.validator.UserGrValidator;
import com.fand.platform.authority.validator.UserGrBuArValidator;
import com.fand.platform.authority.validator.UserGrRoValidator;
import com.fand.platform.authority.validator.UserGrUsValidator;
import com.fand.platform.authority.vo.UserGrVo;
import com.fand.platform.authority.service.AuthorityService;
import com.google.common.collect.Maps;

/**
 * 用户组
 *
 * @author yaoHT
 */
@Controller
public class UserGrController {
	@Qualifier(AuthorityService.BEAN_ID)
	@Autowired(required = false)
	private AuthorityService authorityService;

	@Qualifier(UserGrValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator userGrValidator;

	@Qualifier(UserGrBuArValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator userGrBuArValidator;

	@Qualifier(UserGrRoValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator userGrRoValidator;

	@Qualifier(UserGrUsValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator userGrUsValidator;

	@RequestMapping(value = { "/admin/authority/userGr/userGr/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminUserGrDelete(final UserGrVo userGrVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getUserGrService().deleteUserGrByIds(userGrVo.getUserGr());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGr/edit.html" }, method = RequestMethod.GET)
	public String adminUserGrEdit(final UserGrVo userGrVo, final Model model) {
		final UserGrDto userGr = this.authorityService.getUserGrService().
				getUserGrById(userGrVo.getUserGr());

		model.addAttribute("userGr", userGr);

		return "/admin/authority/userGr/userGr/edit";
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGr.html", "/admin/authority/userGr/userGr/index.html" })
	public String adminUserGrIndex() {
		return "/admin/authority/userGr/userGr/index";
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGr/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminUserGrIndexPagination(final UserGrVo userGrVo) {
		final PaginationDto<Map<String, Object>> userGrPagination = this.authorityService.getUserGrService()
				.paginationUserGrBy1ForMapList(userGrVo.getUserGr());

		final Map<String, Object> userGrPaginationMap = Maps.newHashMap();

		userGrPaginationMap.put("total", userGrPagination.getTotalSize());
		userGrPaginationMap.put("rows", userGrPagination.getDataList());

		return userGrPaginationMap;
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGr/new.html" }, method = RequestMethod.GET)
	public String adminUserGrNew(final UserGrVo userGrVo, final Model model) {
		model.addAttribute("userGr", userGrVo.getUserGr());

		return "/admin/authority/userGr/userGr/new";
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGr/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminUserGrSave(final UserGrVo userGrVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.userGrValidator.validate(userGrVo.getUserGr(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.authorityService.getUserGrService().saveUserGrByIdOrCodeForUserGr(userGrVo.getUserGr());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGr/validationBy1.json" })
	public @ResponseBody
	Boolean adminUserGrValidationBy1(final UserGrVo userGrVo) {
		try {
			final int count = this.authorityService.getUserGrService().readUserGrByCodeNqIdForInteger(
					userGrVo.getUserGr());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGr/view.html" }, method = RequestMethod.GET)
	public String adminUserGrView(final UserGrVo userGrVo, final Model model) {
		final UserGrDto userGr = this.authorityService.getUserGrService().
					getUserGrById(userGrVo.getUserGr());

		model.addAttribute("userGr", userGr);

		return "/admin/authority/userGr/userGr/view";
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrBuAr/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminUserGrBuArDelete(final UserGrVo userGrVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getUserGrService().deleteUserGrBuArByIds(userGrVo.getUserGrBuAr());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrBuAr/edit.html" }, method = RequestMethod.GET)
	public String adminUserGrBuArEdit(final UserGrVo userGrVo, final Model model) {
		final UserGrBuArDto userGrBuAr = this.authorityService.getUserGrService().
				getUserGrBuArById(userGrVo.getUserGrBuAr());

		model.addAttribute("userGrBuAr", userGrBuAr);

		return "/admin/authority/userGr/userGrBuAr/edit";
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrBuAr.html", "/admin/authority/userGr/userGrBuAr/index.html" })
	public String adminUserGrBuArIndex() {
		return "/admin/authority/userGr/userGrBuAr/index";
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrBuAr/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminUserGrBuArIndexPagination(final UserGrVo userGrVo) {
		final PaginationDto<Map<String, Object>> userGrBuArPagination = this.authorityService.getUserGrService()
				.paginationUserGrBuArBy1ForMapList(userGrVo.getUserGrBuAr());

		final Map<String, Object> userGrBuArPaginationMap = Maps.newHashMap();

		userGrBuArPaginationMap.put("total", userGrBuArPagination.getTotalSize());
		userGrBuArPaginationMap.put("rows", userGrBuArPagination.getDataList());

		return userGrBuArPaginationMap;
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrBuAr/new.html" }, method = RequestMethod.GET)
	public String adminUserGrBuArNew(final UserGrVo userGrVo, final Model model) {
		model.addAttribute("userGrBuAr", userGrVo.getUserGrBuAr());

		return "/admin/authority/userGr/userGrBuAr/new";
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrBuAr/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminUserGrBuArSave(final UserGrVo userGrVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.userGrBuArValidator.validate(userGrVo.getUserGrBuAr(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.authorityService.getUserGrService().saveUserGrBuArByIdOrCodeForUserGrBuAr(userGrVo.getUserGrBuAr());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrBuAr/validationBy1.json" })
	public @ResponseBody
	Boolean adminUserGrBuArValidationBy1(final UserGrVo userGrVo) {
		try {
			final int count = this.authorityService.getUserGrService().readUserGrBuArByCodeNqIdForInteger(
					userGrVo.getUserGrBuAr());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrBuAr/view.html" }, method = RequestMethod.GET)
	public String adminUserGrBuArView(final UserGrVo userGrVo, final Model model) {
		final UserGrBuArDto userGrBuAr = this.authorityService.getUserGrService().
					getUserGrBuArById(userGrVo.getUserGrBuAr());

		model.addAttribute("userGrBuAr", userGrBuAr);

		return "/admin/authority/userGr/userGrBuAr/view";
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrRo/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminUserGrRoDelete(final UserGrVo userGrVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getUserGrService().deleteUserGrRoByIds(userGrVo.getUserGrRo());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrRo/edit.html" }, method = RequestMethod.GET)
	public String adminUserGrRoEdit(final UserGrVo userGrVo, final Model model) {
		final UserGrRoDto userGrRo = this.authorityService.getUserGrService().
				getUserGrRoById(userGrVo.getUserGrRo());

		model.addAttribute("userGrRo", userGrRo);

		return "/admin/authority/userGr/userGrRo/edit";
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrRo.html", "/admin/authority/userGr/userGrRo/index.html" })
	public String adminUserGrRoIndex() {
		return "/admin/authority/userGr/userGrRo/index";
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrRo/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminUserGrRoIndexPagination(final UserGrVo userGrVo) {
		final PaginationDto<Map<String, Object>> userGrRoPagination = this.authorityService.getUserGrService()
				.paginationUserGrRoBy1ForMapList(userGrVo.getUserGrRo());

		final Map<String, Object> userGrRoPaginationMap = Maps.newHashMap();

		userGrRoPaginationMap.put("total", userGrRoPagination.getTotalSize());
		userGrRoPaginationMap.put("rows", userGrRoPagination.getDataList());

		return userGrRoPaginationMap;
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrRo/new.html" }, method = RequestMethod.GET)
	public String adminUserGrRoNew(final UserGrVo userGrVo, final Model model) {
		model.addAttribute("userGrRo", userGrVo.getUserGrRo());

		return "/admin/authority/userGr/userGrRo/new";
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrRo/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminUserGrRoSave(final UserGrVo userGrVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.userGrRoValidator.validate(userGrVo.getUserGrRo(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.authorityService.getUserGrService().saveUserGrRoByIdOrCodeForUserGrRo(userGrVo.getUserGrRo());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrRo/validationBy1.json" })
	public @ResponseBody
	Boolean adminUserGrRoValidationBy1(final UserGrVo userGrVo) {
		try {
			final int count = this.authorityService.getUserGrService().readUserGrRoByCodeNqIdForInteger(
					userGrVo.getUserGrRo());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrRo/view.html" }, method = RequestMethod.GET)
	public String adminUserGrRoView(final UserGrVo userGrVo, final Model model) {
		final UserGrRoDto userGrRo = this.authorityService.getUserGrService().
					getUserGrRoById(userGrVo.getUserGrRo());

		model.addAttribute("userGrRo", userGrRo);

		return "/admin/authority/userGr/userGrRo/view";
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrUs/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminUserGrUsDelete(final UserGrVo userGrVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getUserGrService().deleteUserGrUsByIds(userGrVo.getUserGrUs());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrUs/edit.html" }, method = RequestMethod.GET)
	public String adminUserGrUsEdit(final UserGrVo userGrVo, final Model model) {
		final UserGrUsDto userGrUs = this.authorityService.getUserGrService().
				getUserGrUsById(userGrVo.getUserGrUs());

		model.addAttribute("userGrUs", userGrUs);

		return "/admin/authority/userGr/userGrUs/edit";
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrUs.html", "/admin/authority/userGr/userGrUs/index.html" })
	public String adminUserGrUsIndex() {
		return "/admin/authority/userGr/userGrUs/index";
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrUs/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminUserGrUsIndexPagination(final UserGrVo userGrVo) {
		final PaginationDto<Map<String, Object>> userGrUsPagination = this.authorityService.getUserGrService()
				.paginationUserGrUsBy1ForMapList(userGrVo.getUserGrUs());

		final Map<String, Object> userGrUsPaginationMap = Maps.newHashMap();

		userGrUsPaginationMap.put("total", userGrUsPagination.getTotalSize());
		userGrUsPaginationMap.put("rows", userGrUsPagination.getDataList());

		return userGrUsPaginationMap;
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrUs/new.html" }, method = RequestMethod.GET)
	public String adminUserGrUsNew(final UserGrVo userGrVo, final Model model) {
		model.addAttribute("userGrUs", userGrVo.getUserGrUs());

		return "/admin/authority/userGr/userGrUs/new";
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrUs/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminUserGrUsSave(final UserGrVo userGrVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.userGrUsValidator.validate(userGrVo.getUserGrUs(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.authorityService.getUserGrService().saveUserGrUsByIdOrCodeForUserGrUs(userGrVo.getUserGrUs());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrUs/validationBy1.json" })
	public @ResponseBody
	Boolean adminUserGrUsValidationBy1(final UserGrVo userGrVo) {
		try {
			final int count = this.authorityService.getUserGrService().readUserGrUsByCodeNqIdForInteger(
					userGrVo.getUserGrUs());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/authority/userGr/userGrUs/view.html" }, method = RequestMethod.GET)
	public String adminUserGrUsView(final UserGrVo userGrVo, final Model model) {
		final UserGrUsDto userGrUs = this.authorityService.getUserGrService().
					getUserGrUsById(userGrVo.getUserGrUs());

		model.addAttribute("userGrUs", userGrUs);

		return "/admin/authority/userGr/userGrUs/view";
	}
}
